﻿using System;
using Abp.AutoMapper;
using Abp.Dependency;
using Abp.Domain.Uow;
using Abp.Modules;
using Abp.Reflection.Extensions;
using Abp.Runtime.Session;
using Hicap.AuthorizationServer.Core.Caches.Sys;
using Hicap.AuthorizationServer.Core.Caches.Sys.Interface;
using Hicap.AuthorizationServer.Core.Common;
using Hicap.AuthorizationServer.Core.Localization;
using Hicap.AuthorizationServer.Core.Notify;

namespace Hicap.AuthorizationServer.Core
{
    [DependsOn(typeof(AbpAutoMapperModule))]
    public class AuthorizationServerCoreModule : AbpModule
    {
        public override void PreInitialize()
        {
            Configuration.Auditing.IsEnabledForAnonymousUsers = true;
            Configuration.UnitOfWork.OverrideFilter(AbpDataFilters.MustHaveTenant, false);
            Configuration.UnitOfWork.OverrideFilter(AbpDataFilters.MayHaveTenant, false);

            AuthorizationServerLocalizationConfigurer.Configure(Configuration.Localization);

            Configuration.ReplaceService(typeof(IAbpSession), () =>
                {
                    IocManager.Register<IAbpSession, HicapSession>(DependencyLifeStyle.Transient);
                });
            Configuration.ReplaceService(typeof(IPrincipalAccessor), () =>
            {
                IocManager.Register<IPrincipalAccessor, HicapPrincipalAccessor>(DependencyLifeStyle.Transient);
            });

            Configuration.Caching.Configure("UserAuthCache", cache =>
            {
                cache.DefaultSlidingExpireTime = TimeSpan.FromMinutes(5);
            });
        }

        public override void Initialize()
        {
            IocManager.RegisterAssemblyByConvention(typeof(AuthorizationServerCoreModule).GetAssembly());
            IocManager.Register<IUserAuthCache, UserAuthCache>(DependencyLifeStyle.Transient);
            IocManager.Register<NotifyHub>(DependencyLifeStyle.Singleton);
        }
    }
}